//
//  HWGameDetailTradeCell.swift
//  HWanGame
//
//  Created by 王昌健 on 2022/3/25.
//  Copyright © 2022 94Wan. All rights reserved.
//

import UIKit

class HWGameDetailTradeOneYuanCell: UITableViewCell {
    var bgView:UIView!
    var gameImageView:UIImageView!
    var titleLabel:UILabel!
    var contentLabel:UILabel!
    var bottomLabel:UILabel!
    
    var consumeLabel:UILabel!
    var consume:UILabel!
    var priceLabel:UILabel!
    var price:UILabel!
    
    var lineView:UIView!
    
    var oldPriceLabel:UILabel!
    var isOneYuan = false
    var model:HWGameDetailTradeModel?{
        didSet {
//            timeLabel.text = "上架时间：" + String.timeIntervalChangeToTimeStr(timeInterval: model?.create_time ?? 0, "yyyy-MM-dd")
            gameImageView.kf.setImage(with: URL(string: model?.icon ?? ""))
            titleLabel.text = model?.name
            
            contentLabel.text = model?.introduction
            bottomLabel.text = "#\(model?.gname ?? "")#、#\(model?.system_type ?? "")#"
            price.text = model?.money
            consume.text = "\(model?.leiji ?? "") 元"
            
            if isOneYuan {
                let oldPrice = "原价：\(model?.yuanjia ?? "")"
                let attri = NSMutableAttributedString(string: oldPrice)
                attri.addAttribute(NSAttributedString.Key.strikethroughStyle, value:2 , range: NSMakeRange(0,oldPrice.count))
                attri.addAttribute(.strikethroughColor, value: UIColor.colorWithHexString(hexString: "#CCCCCC"), range:  NSMakeRange(0,oldPrice.count))
                oldPriceLabel.attributedText = attri
                
            }
        }
    }
    
//    var oneModel:HWGameDetailTradeModel? {
//        didSet {
//            gameImageView.kf.setImage(with: URL(string: oneModel?.icon ?? ""))
//            titleLabel.text = oneModel?.biaoti
//
//            contentLabel.text = oneModel?.jieshao
//            bottomLabel.text = "#\(oneModel?.gname ?? "")#、#\(oneModel?.system_type ?? "")#"
//            price.text = oneModel?.csjg
//            consume.text = "\(oneModel?.pay ?? "") 元"
//
//
//            let oldPrice = "原价：\(oneModel?.paymoney ?? "")"
//            let attri = NSMutableAttributedString(string: oldPrice)
//            attri.addAttribute(NSAttributedString.Key.strikethroughStyle, value:2 , range: NSMakeRange(0,oldPrice.count))
//            attri.addAttribute(.strikethroughColor, value: UIColor.colorWithHexString(hexString: "#CCCCCC"), range:  NSMakeRange(0,oldPrice.count))
//            oldPriceLabel.attributedText = attri
////            attributeString.addAttribute(NSAttributedString.Key.baselineOffset, value: 0, range: totalNsString.range(of: strikeString))
//            //下面开始是设置线条的风格：
//            //第一个参数addAttribute:是设置要中线（删除线）还是下划线。
//            //NSStrikethroughStyleAttributeName：这种是从文本中间穿过，也就是删除线。
//            //NSUnderlineStyleAttributeName：这种是下划线。
//
//            //第二个参数value：是设置线条的风格：虚线，实现，点线......
//            //第二参数需要同时设置Pattern和style才能让线条显示。
//
//            //第三个参数range:是设置线条的长度，切记，不能超过字符串的长度，否则会报错。
////            [attri addAttribute:NSStrikethroughStyleAttributeName value:@(NSUnderlinePatternSolid | NSUnderlineStyleSingle)  range:NSMakeRange(0, length)];
//
//            //下列是设置线条的颜色
//            //第一个参数就是选择设置中线的颜色还是下划线的颜色，如果上面选择的是中线，这里就要选择中线，否则颜色设置不上去。
//            //第二个参数很简单，就是颜色而已。
//            //第三个参数：同上。
////            [attri addAttribute:NSStrikethroughColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, length)];
////
////            [label setAttributedText:attri];
//        }
//    }
    class func cellWithTableView(tableView:UITableView) -> (HWGameDetailTradeOneYuanCell) {
        let reUse:String = "HWGameDetailTradeOneYuanCell"
        
        var cell = tableView.dequeueReusableCell(withIdentifier: reUse)
        
        if cell == nil {
            cell = HWGameDetailTradeOneYuanCell.init(style: UITableViewCell.CellStyle.default, reuseIdentifier: reUse)
        }
        
        return cell as! HWGameDetailTradeOneYuanCell
    }
    
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        initUI()
        initFrame()
    }
    override func layoutSubviews() {
        super.layoutSubviews()
        bgView.layer.shadowColor = UIColor.colorWithAlphaHexString(hexString: "#E7E7E7", alpha: 1).cgColor
        bgView.layer.shadowOffset = CGSize(width: 0, height: 0)
        bgView.layer.shadowRadius = 9*widthScale
        bgView.layer.shadowOpacity = 1
        bgView.layer.cornerRadius = 7*widthScale
    }
    func initUI() -> Void {
        selectionStyle = UITableViewCell.SelectionStyle.none
        backgroundColor = UIColor.clear
        
        bgView = UIView()
        bgView.backgroundColor = .white
        contentView.addSubview(bgView)
        
        gameImageView = UIImageView()
        gameImageView.contentMode = .scaleAspectFill
        gameImageView.layer.masksToBounds = true
        gameImageView.layer.cornerRadius = 12*widthScale
        bgView.addSubview(gameImageView)
        
        titleLabel = HWLabel.createBoldLabel(title: "", color: "#333333", font: 14*widthScale)
        titleLabel.numberOfLines = 1
        bgView.addSubview(titleLabel)
        
        contentLabel = HWLabel.createLabel(title: "", color: "#888888", font: 11*widthScale)
        contentLabel.numberOfLines = 1
        bgView.addSubview(contentLabel)
        
        bottomLabel = HWLabel.createLabel(title: "", color: "#FD9F65", font: 11*widthScale)
        bottomLabel.numberOfLines = 1
        bgView.addSubview(bottomLabel)
        
        consumeLabel = HWLabel.createLabel(title: "累计消费：", color: "#888888", font: 11*widthScale)
        consumeLabel.numberOfLines = 1
        bgView.addSubview(consumeLabel)
        
        consume = HWLabel.createLabel(title: "", color: "#FF726A", font: 11*widthScale)
        consume.numberOfLines = 1
        bgView.addSubview(consume)
        
        priceLabel = HWLabel.createBoldLabel(title: "￥", color: "#FF726A", font: 11*widthScale)
        bgView.addSubview(priceLabel)
        
        price = HWLabel.createBoldLabel(title: "", color: "#FF726A", font: 16*widthScale)
        bgView.addSubview(price)
        
        oldPriceLabel = HWLabel.createBoldLabel(title: "", color: "#CCCCCC", font: 11*widthScale)
        bgView.addSubview(oldPriceLabel)
        
        lineView = UIView()
        lineView.frame = CGRect(x: 0, y: 0, width: 321*widthScale, height: 1*widthScale)
        bgView.addSubview(lineView)
        
        let shapeLayer:CAShapeLayer = CAShapeLayer()
        
        shapeLayer.bounds = lineView.bounds
        
        shapeLayer.position = CGPoint(x: lineView.frame.width / 2, y: lineView.frame.height / 2)
        
        shapeLayer.fillColor = UIColor.clear.cgColor
        shapeLayer.strokeColor = UIColor.colorWithAlphaHexString(hexString: "#979797", alpha: 0.5).cgColor
        
        shapeLayer.lineWidth = 1*widthScale
        shapeLayer.lineJoin = .round
        shapeLayer.lineDashPhase = 0
        shapeLayer.lineDashPattern = [NSNumber(value: 1*widthScale), NSNumber(value: 1*widthScale)]
        
        let path:CGMutablePath = CGMutablePath()
        path.move(to: CGPoint(x: 0, y: 0))
        path.addLine(to: CGPoint(x: lineView.frame.width, y: 0))
        shapeLayer.path = path
        
        lineView.layer.addSublayer(shapeLayer)
        
//        drawDashLine(lineView, strokeColor: .colorWithHexString(hexString: "#979797"), lineWidth: 1, lineLength: 10, lineSpacing: 19)
    }
    
    func initFrame() -> Void {
        bgView.snp.makeConstraints { make in
            make.left.equalTo(18*widthScale)
            make.right.equalTo(-19)
            make.top.equalTo(9*widthScale).priority(.high)
            make.bottom.equalToSuperview()
        }
        
        gameImageView.snp.makeConstraints { make in
            make.left.top.equalTo(9*widthScale)
            make.width.height.equalTo(63*widthScale)
            make.bottom.equalTo(-50*widthScale)
        }
        
        
        titleLabel.snp.makeConstraints { make in
            make.left.equalTo(gameImageView.snp.right).offset(10*widthScale)
            make.top.equalTo(gameImageView.snp.top).offset(2*widthScale)
            make.right.equalTo(-15*widthScale)
            make.height.equalTo(20*widthScale)
        }
        
        contentLabel.snp.makeConstraints { make in
            make.left.equalTo(titleLabel)
            make.top.equalTo(titleLabel.snp.bottom).offset(5*widthScale)
            make.right.equalTo(-15*widthScale)
            make.height.equalTo(15*widthScale)
        }
        
        bottomLabel.snp.makeConstraints { make in
            make.left.equalTo(contentLabel)
            make.top.equalTo(contentLabel.snp.bottom).offset(5*widthScale)
            make.right.equalTo(-15*widthScale)
            make.height.equalTo(15*widthScale)
        }
        
        lineView.snp.makeConstraints { make in
            make.left.equalTo(9*widthScale)
            make.top.equalTo(gameImageView.snp.bottom).offset(8*widthScale)
            make.height.equalTo(1)
            make.right.equalTo(-9*widthScale)
        }
        
        consumeLabel.snp.makeConstraints { make in
            make.left.equalTo(9*widthScale)
            make.bottom.equalTo(-13*widthScale)
            make.height.equalTo(15*widthScale)
        }
        
        consume.snp.makeConstraints { make in
            make.left.equalTo(consumeLabel.snp.right).offset(5*widthScale)
            make.bottom.equalTo(-13*widthScale)
            make.height.equalTo(15*widthScale)
        }
        
        price.snp.makeConstraints { make in
            make.right.equalTo(-9*widthScale)
            make.bottom.equalTo(-9*widthScale)
            make.height.equalTo(23*widthScale)
        }
        
        priceLabel.snp.makeConstraints { make in
            make.right.equalTo(price.snp.left)
            make.bottom.equalTo(price.snp.bottom).offset(-2*widthScale)
            make.height.equalTo(15*widthScale)
        }
        
        oldPriceLabel.snp.makeConstraints { make in
            make.centerY.equalTo(priceLabel)
            make.right.equalTo(priceLabel.snp.left).offset(-9*widthScale)
        }
        
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}
